package leetcode.light;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * MerchantFlag
 *
 * @author lichx
 * @createTime 2024/7/31 10:34
 */
public class MerchantFlag {

    private final static char[] VOWEL_ARRAY = new char[]{'a', 'e', 'i', 'o', 'u', 'y'};

    private final static char[] CONSONANT_ARRAY = new char[]{'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n'
            , 'p', 'q', 'r', 's', 't',  'v', 'w', 'x', 'z'};

    private final static int NAME_LENGTH = 8;

    private final static int VOWEL_NUM = 4;

    private final static int NAME_COUNT = 1;

    public static void main(String[] args) throws IOException {
        File file = new File("D:\\merchantFlag.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream stream = new FileOutputStream(file);
        FileChannel channel = stream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(NAME_LENGTH+1);

        String str;
        for (int i = 0; i < NAME_COUNT; i++) {

            str = randomName();

            buffer.clear();
            buffer.put(str.getBytes());
            buffer.flip();
            channel.write(buffer);
        }

        channel.close();
    }

    private static final String randomName() {
        StringBuilder sb = new StringBuilder();
        int vowelCount = VOWEL_NUM;
        for (int i = NAME_LENGTH; i > 0; i--) {
            int random = (int)(Math.random() *(i));
            if (random < vowelCount) {
                vowelCount--;
                sb.append(VOWEL_ARRAY[(int) (VOWEL_ARRAY.length * Math.random())]);
            }else {
                sb.append(CONSONANT_ARRAY[(int)(CONSONANT_ARRAY.length*Math.random())]);
            }
        }
        return sb.append("\n").toString();
    }
}
